
/*
 *************************************************************************
 *西瑞克斯研发管理系统
 *版 本: 1.0   
 *日 期：02/08/2012    
 *作 者： 王浩 QQ：873987060   
 *************************************************************************   
 */
package com.application.research1bm.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.application.research1bm.entity.Research1bm;
import com.application.research1bm.struts.form.Research1bmForm;
import com.application.usertable.entity.Usertable;
import com.application.yfproject.entity.YfProject;
import com.manager.BaseDao;
import com.manager.commons.CommonUtil;
import com.manager.commons.CurrentUserUtils;
import com.manager.commons.DateUtil;
import com.manager.commons.dao.HibernateGenericDao;
import com.manager.commons.page.PageBean;

/**
 * 
 * Description:Research1bmDAO<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 左磊 QQ：30575951 <br />
 * 
 * History: <br />
 * 
 * 02/08/2012
 * 
 */
public class Research1bmDAO extends HibernateGenericDao<Research1bm> {
	private static final Logger logger = Logger.getLogger(Research1bmDAO.class);

	private Research1bm research1bm;

	public Research1bm getResearch1bm() {
		return this.research1bm;
	}

	public void setResearch1bm(Research1bm research1bm) {
		this.research1bm = research1bm;
	}

	public Integer getAllCountResearch1bm(Research1bmForm research1bmForm) {
		// 储存查询条件的buffer
		List args = new ArrayList();
		StringBuffer buf = new StringBuffer();

		buf
				.append("SELECT COUNT(0) FROM research1bm t left join yf_project p on t.project_no = p.project_no  left join usertable u on p.project_manager = u.id left join department b on p.project_org = b.id WHERE 1=1  ");
		// 查询字符串
		buf = getStringBuffer(research1bmForm, buf, args);
		return super.getCount(buf.toString(), args.toArray());
	}

	public void deleteResearch1bms(Research1bmForm research1bmForm) {
		String a = research1bmForm.getIds().replaceAll(",", "','");
		String hql = " delete from Research1bm where id in  ( '" + a + "')";
		Session session = this.getSession();
		try {
			session.createQuery(hql).executeUpdate();
		} catch (RuntimeException re) {
			logger.error("deleteResearch1bms failed");
			throw re;
		} finally {
			if (session != null) {
				releaseSession(session);
			}
		}
	}

	public List searchResearch1bm(final Research1bmForm research1bmForm,
			final PageBean pageBean) {

		List args = new ArrayList();
		StringBuffer buf = new StringBuffer();
		buf
				.append("SELECT t.*,u.rname as uname,p.project_org ,p.project_name,b.name as orgname FROM research1bm t left join yf_project p on t.project_no = p.project_no  left join usertable u on p.project_manager = u.id left join department b on p.project_org = b.id WHERE 1=1  ");
		buf = getStringBuffer(research1bmForm, buf, args);
		buf.append(" order by t.createDate Desc ");
		return getPageInfo(pageBean, buf.toString(), args);
	}

	public List searchResearch1bm(final Research1bmForm research1bmForm) {
		List args = new ArrayList();
		StringBuffer buf = new StringBuffer();
		buf
				.append("SELECT t.*,u.rname as uname,p.project_org,p.project_name,b.name as orgname   FROM research1bm t left join yf_project p on t.project_no = p.project_no  left join usertable u on p.project_manager = u.id left join department b on p.project_org = b.id WHERE 1=1  ");
		buf = getStringBuffer(research1bmForm, buf, args);
		buf.append(" order by t.createDate Desc ");
		return getPageInfo(null, buf.toString(), args);
	}

	/**
	 * 查询条件的封装
	 * 
	 * @param research1bmForm
	 * @return
	 */
	private StringBuffer getStringBuffer(Research1bmForm research1bmForm,
			StringBuffer buf, List args) {
		/*
		 * 需要加入查询条件时封装
		 */
		if (StringUtils.isNotBlank(research1bmForm.getId())) {
			buf.append(" and t.id = '" + research1bmForm.getId().trim() + "'");
		} else {
			if (StringUtils.isNotBlank(research1bmForm.getProjectNo())) {
				buf.append(" and t.project_no = '"
						+ research1bmForm.getProjectNo().trim() + "'");
			}
			if (StringUtils.isNotBlank(research1bmForm.getProjectOrg())) {
				buf.append(" and p.project_org = '"
						+ research1bmForm.getProjectOrg().trim() + "'");
			}
			if (StringUtils.isNotBlank(research1bmForm.getProjectName())) {
				buf.append(" and p.project_name like '%"
						+ research1bmForm.getProjectName().trim() + "%'");
			}
			if (StringUtils.isNotBlank(research1bmForm.getUname())) {
				buf.append(" and u.rname like '%"
						+ research1bmForm.getUname().trim() + "%'");
			}
			if (StringUtils.isNotBlank(research1bmForm.getOperateid())) {
				buf.append(" and t.operateid = '"
						+ research1bmForm.getOperateid() + "'");
			}
			if (StringUtils.isNotBlank(research1bmForm.getCreateDateState())) {
				buf.append(" and t.createDate > '"
						+ research1bmForm.getCreateDateState() + "'");
			}
			if (StringUtils.isNotBlank(research1bmForm.getCreateDateState())) {
				buf.append(" and t.createDate < '"
						+ research1bmForm.getCreateDateEnd() + "'");
			}
		}
		buf = CommonUtil.filterData(buf, args);
		return buf;
	}
}
